package itsm.isperp.module.controller.task;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.task.TskInspectTemplate;
import itsm.isperp.module.entity.task.TskInspectTemplateCheck;
import itsm.isperp.module.service.task.TskInspectTemplateCheckService;
import itsm.isperp.module.service.task.TskInspectTemplateService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.chanjar.weixin.common.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2015-11-06 11:09
 */
@Controller
@RequestMapping("task/inspectTemplateCheck")
public class TskInspectTemplateCheckController extends
		BaseController<TskInspectTemplateCheck> {

	@Autowired
	protected TskInspectTemplateCheckService tskInspectTemplateCheckService;

	@Override
	public TskInspectTemplateCheckService getService() {
		return this.tskInspectTemplateCheckService;
	}

	@Override
	public String getPageTitle() {
		return "巡检模板定义 ";
	}

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {

		ModelAndView mv = this.getEdit(id);
		TskInspectTemplateCheck model = (TskInspectTemplateCheck) mv
				.getModelMap().get("model");
		String templateId = req.getParameter("templateId");

		if (StringUtils.isNotEmpty(templateId)) {

			TskInspectTemplateService tskInspectTemplateService = ContextHolder
					.getSpringBean("tskInspectTemplateService");
			TskInspectTemplate tskInspectTemplate = tskInspectTemplateService
					.findOne(templateId);
			model.setTemplateId(templateId);
			model.setCiId(tskInspectTemplate.getCiId());

		}
		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		if (StringUtils.isEmpty(model.getCheckName())) {

			Integer l = jdbcTemplate.queryForObject(
					"SELECT COUNT(*)+1 FROM tsk_inspect_template_check WHERE template_id='"
							+ model.getTemplateId() + "'", Integer.class);
			model.setOrderNum(l);
			model.setNotnull(true);
		}

		List<Map<String, Object>> checkNames = jdbcTemplate
				.queryForList("SELECT DISTINCT check_name,items,default_value,data_type ,check_unit FROM tsk_inspect_template_check ");
		mv.addObject("checkNames", checkNames);
		return mv;

	}

	@RequestMapping("list")
	@ResponseBody
	@Override
	public PageResponse<TskInspectTemplateCheck> list(JqGridRequest request,
			HttpServletRequest req) {
		String templateId = req.getParameter("templateId");
		if (StringUtils.isNotEmpty(templateId)) {
			request.addFilterRule("templateId", JqGridFilterRuleOp.eq,
					templateId);
		}

		return this.getService().findAll(request).toPage();

	}

	/**
	 * 详细信息列表
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {
		String templateId = req.getParameter("templateId");
		if (StringUtils.isNotEmpty(templateId)) {
			request.addFilterRule("templateId", JqGridFilterRuleOp.eq,
					templateId);
		}

		return this.getService().findAllDetailByRequest(request).toPage();

	}
}
